<template>
  <div class="app-container">
    <!--        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
    <!--          <el-form-item label="查看禁用图片" prop="enable">-->
    <!--            <el-input-->
    <!--              v-model="queryParams.enable"-->
    <!--              placeholder="请选择图片状态"-->
    <!--              clearable-->
    <!--              @keyup.enter.native="handleQuery"-->
    <!--            />-->
    <!--          </el-form-item>-->
    <!--          <el-form-item>-->
    <!--            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
    <!--            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
    <!--          </el-form-item>-->
    <!--        </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:banner:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:banner:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:banner:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:banner:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bannerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号"  width="68"  type="index"/>
<!--      <el-table-column label="超链接地址" align="center" prop="bannerLink">-->
<!--        <template slot-scope="scope">-->
<!--          <a :href="scope.row.bannerLink" target="_blank">{{scope.row.bannerLink}}</a>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="新闻标题" align="center" prop="bannerTitle"></el-table-column>
      <el-table-column label="图片" align="center" prop="filePath">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.filePath"
            :preview-src-list="[scope.row.filePath]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"></el-table-column>
      <el-table-column label="是否显示" align="center" prop="enable">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enable"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            v-hasPermi="['system:news:edit']"
          >内容编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:banner:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:banner:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改轮播图对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="超链接地址" prop="bannerLink">-->
<!--           <span slot="label">-->
<!--                <el-tooltip content="输入的超链接以`http(s)://`开头" placement="top">-->
<!--                <i class="el-icon-question"></i>-->
<!--                </el-tooltip>-->
<!--                超链接地址-->
<!--              </span>-->
<!--          <el-input v-model="form.bannerLink" placeholder="请输入超链接地址"/>-->
<!--        </el-form-item>-->
        <el-form-item label="图片" prop="filePath">
<!--          <div v-model="form.filePath"></div>-->
          <img-upload ref="uploadImage" :deleteBanner="deleteBanner" :uploadFiles="uploadFiles" :value="form.bannerImage"  :limit="1" :handleUpload="handleUpload"></img-upload>
        </el-form-item>
        <el-form-item label="标题" prop="bannerTitle">
          <el-input v-model="form.bannerTitle" placeholder="请输入新闻标题"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="轮播内容编辑" :visible.sync="open2"
               fullscreen
               :close-on-click-modal="false" width="50%" append-to-body
    >
      <MyEditer ref="MyEditer" :newsContent="form.bannerContent"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { listBanner, getBanner, delBanner, addBanner, updateBanner } from '@/api/system/banner'
import imgUpload from '@/components/ImageUpload/index.vue'
import MyEditer from '../../../../components/MyEditer'

export default {
  name: 'Banner',
  components: {
    imgUpload,MyEditer
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 轮播图表格数据
      bannerList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bannerLink: null,
        fileId: null,
        enable: null,
        bannerType:1,
        remark: null,
        bannerTitle:null,
      },
      // 表单参数
      form: {
        filePath: null,
        remark: null,
        bannerTitle:null,
      },
      // 表单校验
      rules: {
        bannerLink: [
         { required: true, message: '请输入超链接地址', trigger: 'blur' }
        ],
        filePath: [
          { required: true, message: '请上传图片' }
        ],
        bannerTitle:[
          { required: true, message: '请输入标题', trigger: 'blur' }
        ]
      },
      baseUrl: process.env.VUE_APP_BASE_API,
      //富文本
      open2:false,
    }
  },
  created() {

    this.getList()
  },
  methods: {
    /** 查询轮播图列表 */
    getList() {
      this.loading = true
      listBanner(this.queryParams).then(response => {
        this.bannerList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.open2 = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bannerLink: null,
        fileId: null,
        enable: null,
        filePath: null,
        bannerType: 1,
        remark: null,
        bannerTitle:null,
      }
      // this.$nextTick(()=>{
      //   this.$refs.uploadImage.clearImage()
      // })
      this.resetForm('form')
    },
    /**子组件返回上传图片*/
    handleUpload(file) {
      this.form.filePath = file.url
    },
    deleteBanner(){
      this.form.filePath = ""
    },

    handleUploadBanner(file){
      this.form.filePath = file
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加轮播图'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getBanner(id).then(response => {
        this.form = response.data
        if(this.form.fileId){
          this.form.bannerImage = [{
            fileId:this.form.fileId,
            path:this.form.filePath
          }]
        }else{
          this.form.bannerImage = []
        }
        console.log(this.form,"bannerImagebannerImage")
        this.open = true
        this.title = '修改轮播图'
      })
    },
    //编辑新闻内容
    handleEdit(row) {
      this.reset()
      const id = row.id || this.ids
      this.form.id = id
      this.open2 = true
      this.$nextTick(()=>{
       setTimeout(()=>{
         this.$refs.MyEditer.initEditor2(id)
       })
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBanner(this.form).then(async response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              // this.$refs.uploadImage1.uploadData.fileType = 3
              // this.$refs.uploadImage1.uploadData.marksId = this.marksId
              // this.$refs.uploadImage1.handleSubmit()
              this.$refs.uploadImage.uploadData.bannerId = this.form.id
              await this.$refs.uploadImage.handleSubmit()
              this.getList()
            })
          } else {
            addBanner(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.$refs.uploadImage.uploadData.bannerId = response.data
              this.$refs.uploadImage.handleSubmit().then(res => {
                this.getList()
                this.open = false
              })
            })
          }
        }
      })
    },
    //提交富文本
    submitForm2() {
      if (this.form.bannerContent != '') {
        this.form.bannerContent = this.$refs.MyEditer.getHtml()
        console.log("修改内容")
        updateBanner(this.form).then(response => {
          this.$modal.msgSuccess('修改成功')
          this.open2 = false
          this.getList()
        })
      } else {
        this.$message.error('请先输入内容！')
      }
    },
    uploadFiles(){
      this.getList()
      this.open = false
      console.log("自组建触发！")
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除轮播图编号为"' + ids + '"的数据项？').then(function() {
        return delBanner(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/banner/export', {
        ...this.queryParams
      }, `banner_${new Date().getTime()}.xlsx`)
    },
    // 轮播图显示状态
    handleStatusChange(row) {
      const form = {
        id: row.id,
        enable: !row.enable == true ? 0 : 1
      }
      updateBanner(form).then(response => {
        this.$modal.msgSuccess('修改成功')
        this.getList()
      })
    }

  }
}
</script>
